#include "ejsidebar.h"
#include <QPaintEvent>
#include <QPainter>
#include <QFont>
#include <QBrush>
#include <QColor>

EjSideBar::EjSideBar(QWidget * parent) : QWidget(parent)
{
    m_checkedAction = nullptr;
    m_pressedAction = nullptr;
}

void EjSideBar::addAction(QAction *action)
{
    m_actions.push_back(action);
    action->setCheckable(true);
    update();
}

QAction *EjSideBar::addAction(const QString &text, const QIcon &icon)
{
    auto action = new QAction(icon, text, this);
    m_actions.push_back(action);
    action->setCheckable(true);
    update();
    return action;
}

void EjSideBar::paintEvent(QPaintEvent *event)
{
    QPainter p(this);

    QFont fontText(p.font());
    fontText.setFamily("Helvetica Neue");
    p.setFont(fontText);

    QImage texture(":/res/png/texture.png");
    p.fillRect(event->rect(), QBrush(texture));
    p.setPen(Qt::black);
    p.drawLine(event->rect().topRight(), event->rect().bottomRight());

    int actions_height = m_actions.size()*action_height;

    int action_y = event->rect().height()/2-actions_height/2;
    foreach(QAction *action, m_actions)
    {
        QRect actionRect(0, action_y, event->rect().width(), action_height);

        if(action->isChecked())
        {
            p.setOpacity(0.5);
            p.fillRect(actionRect, QColor(19, 19, 19));
            p.setPen(QColor(9, 9, 9));
            p.drawLine(actionRect.topLeft(), actionRect.topRight());
            p.setOpacity(1);

        }

        if(action == m_actions.last())
        {
            p.setPen(QColor(15, 15, 15));
            p.drawLine(QPoint(0, actionRect.bottomLeft().y()-1), QPoint(actionRect.width(), actionRect.bottomRight().y()-1));
            p.setPen(QColor(55, 55, 55));
            p.drawLine(actionRect.bottomLeft(), actionRect.bottomRight());

        }

        if(!action->isChecked())
        {
            p.setPen(QColor(15, 15, 15));
            p.drawLine(actionRect.topLeft(), actionRect.topRight());
            p.setPen(QColor(55, 55, 55));
            p.drawLine(QPoint(0, actionRect.topLeft().y()+1), QPoint(actionRect.width(), actionRect.topRight().y()+1));
        }

        QRect actionIconRect(0, action_y, event->rect().width(), action_height-20);
        QIcon  actionIcon(action->icon());
        actionIcon.paint(&p, actionIconRect);


        p.setPen(QColor(217, 217, 217));
        if(action->isChecked())
            p.setPen(QColor(255, 255, 255));
        QRect actionTextRect(0, action_y+actionRect.height()-20, event->rect().width(), 15);
        p.drawText(actionTextRect, Qt::AlignCenter, action->text());

        action_y += actionRect.height();
    }
}

void EjSideBar::mousePressEvent(QMouseEvent *event)
{
    m_pressedAction = actionAt(event->pos());
    if(m_pressedAction == nullptr || m_pressedAction == m_checkedAction)
        return;
    update();
}

void EjSideBar::mouseReleaseEvent(QMouseEvent *event)
{
    QAction* tempAction = actionAt(event->pos());
    if(m_pressedAction != tempAction || tempAction == nullptr)
    {
        m_pressedAction = nullptr;
        return;
    }
    if(m_checkedAction != nullptr)
        m_checkedAction->setChecked(false);
    m_checkedAction = m_pressedAction;
    if(m_checkedAction != nullptr)
    {
        m_checkedAction->activate(QAction::Trigger);
        m_checkedAction->setChecked(true);
    }
    update();
    m_pressedAction = nullptr;
}

QSize EjSideBar::minimumSizeHint() const
{
    return QSize(90, m_actions.size()*action_height);
}

QAction *EjSideBar::actionAt(const QPoint &at)
{
    int actions_height = m_actions.size()*action_height;

    int action_y = rect().height()/2-actions_height/2;
    foreach(QAction *action, m_actions)
    {
        QRect actionRect(0, action_y, rect().width(), action_height);
        if(actionRect.contains(at))
            return action;
        action_y += actionRect.height();
    }
    return nullptr;
}
